package com.cmsz.collection.repository;

import com.cmsz.collection.bean.AlarmLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Service;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@Service
public class AlarmLogRepository {

    @Autowired
    private final JdbcTemplate jdbcTemplate;

    public AlarmLogRepository(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    public int batchCreateAlarmLog(List<AlarmLog> list) {
    String sql = "INSERT INTO cmsz_alarm_log(appName, content, createdTime) VALUES(?, ?, ?)";

//        jdbcTemplate.batchUpdate(sql, (BatchPreparedStatementSetter) list);

        return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
            AlarmLog alarmLog = list.get(i);
            System.err.println(alarmLog.getCreatedTime().toString());
            preparedStatement.setString(1, alarmLog.getAppName());
            preparedStatement.setString(2, alarmLog.getContent());
            preparedStatement.setString(3, alarmLog.getCreatedTime().toString());
        }

        @Override
        public int getBatchSize() {
            return list.size();
        }
    }).length;

}
}
